package cn.edu.sdjzu.xg.bysj.service;

import cn.edu.sdjzu.xg.bysj.dao.DegreeDao;
import cn.edu.sdjzu.xg.bysj.domain.Degree;
import util.Condition;
import util.JdbcHelper;
import util.Pagination;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

public final class DegreeService {
    //获得degreeDao实例
    private static DegreeDao degreeDao = DegreeDao.getInstance();
    //新建degreeService对象
    private static DegreeService degreeService =new DegreeService();
    //构造器
    DegreeService(){}
    //获得degreeService实例
    public static DegreeService getInstance(){
        return degreeService;
    }

    /**
     * 查找所有学位信息
     *
     * @param condition
     * @param pagination
     *
     * @return
     * @throws SQLException
     */
    public Collection<Degree> findAll(Collection<Condition> condition, Pagination pagination) throws SQLException {
        //获得数据库链接
        Connection connection = JdbcHelper.getConn();
        Collection<Degree> degrees = null;
        //关闭自动提交，事务开始
        connection.setAutoCommit(false);
        try {
            degrees = degreeDao.findAll(connection,condition,pagination);
            //提交事务
            connection.commit();
        }
        catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        }
        finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return degrees;
    }


    /**
     * 根据id查找学位信息
     *
     * @param id
     *
     * @return
     * @throws SQLException
     */
    public Degree find(Integer id) throws SQLException {
        //获得数据库链接
        Connection connection = JdbcHelper.getConn();
        Degree degree = null;
        //关闭自动提交，事务开始
        connection.setAutoCommit(false);
        try {
            degree = degreeDao.find(connection,id);
            //提交事务
            connection.commit();
        }
        catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        }
        finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return degree;
    }

    /**
     * 更新学位信息
     *
     * @param degree
     *
     * @return
     * @throws SQLException
     */
    public boolean update(Degree degree) throws SQLException {
        //获得数据库链接
        Connection connection = JdbcHelper.getConn();
        boolean updated =false;
        //关闭自动提交，事务开始
        connection.setAutoCommit(false);
        try {
            updated = degreeDao.update(connection,degree);
            //提交事务
            connection.commit();
        }
        catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        }
        finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return updated;
    }

    //添加学位
    public boolean add(Degree degree) throws SQLException {
        Connection connection = JdbcHelper.getConn();
        boolean added = degreeDao.add(connection,degree);
        //关闭自动提交，事务开始
        connection.setAutoCommit(false);
        try {
            added = degreeDao.add(connection,degree);
            //提交事务
            connection.commit();
        }
        catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        }
        finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return added;                                                                                     
    }

    /**
     * 删除学位信息
     *
     * @param id
     *
     * @return
     * @throws SQLException
     */
    public boolean delete(Integer id) throws SQLException {
        //获得数据库链接
        Connection connection = JdbcHelper.getConn();
        boolean deleted = degreeDao.delete(connection,id);
        //关闭自动提交，事务开始
        connection.setAutoCommit(false);
        try {
            deleted = degreeDao.delete(connection,id);
            //提交事务
            connection.commit();
        }
        catch (SQLException e) {
            //回滚事务中所有操作
            connection.rollback();
        }
        finally {
            //将自动提交设置为true，结束事务
            connection.setAutoCommit(true);
            //释放连接
            JdbcHelper.close(connection);
        }
        return deleted;
    }
}

